package com.itheima.service;

import com.itheima.dao.BaseStudentDao;
import com.itheima.dao.OtherStudentDao;
import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
import com.itheima.factory.StudentFactory;

/*
学生 业务处理类

增
删
改
查
 */
public class StudentService {
    private BaseStudentDao studentDao = StudentFactory.getStudentDao();
    public boolean addStudent(Student student) {

        boolean flag = studentDao.addStudent(student);
        return flag;
    }

    public boolean isExists(String sid) {
        //真正的做判断
        //获取所有的学生

        Student[] students = studentDao.findAllStudents();
        // 遍历所有学生
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getSid().equals(sid)) {
                //判断 学号和 录入的学号 是否相同
                //相同 存在 返回 true
                return true;
            }
        }
        return false;
    }

    public Student[] findAllStudents() {

        Student[] students = studentDao.findAllStudents();
        //判断 数组 是否为null
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if(student!=null){
                return students;
            }
        }
        //不为空 返回数组
        //为null  返回null
        return null;

    }

    public void deleteStudent(String sid) {
        studentDao.deleteStudent(sid);
    }

    public void updateStudent(String sid, Student student) {
        studentDao.updateStudent(sid,student);
    }
}
